package com.leadingsoft.web.core.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leadingsoft.common.utils.MD5Utils;
import com.leadingsoft.core.mapper.AccessMapper;
import com.leadingsoft.core.mapper.UserInfoMapper;
import com.leadingsoft.core.pojo.Access;
import com.leadingsoft.core.pojo.AccessExample;
import com.leadingsoft.core.pojo.UserInfo;
import com.leadingsoft.core.pojo.UserInfoExample;
import com.leadingsoft.core.pojo.UserInfoExample.Criteria;

@Service
public class CoreService {
	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private AccessMapper accessMapper;

	/**
	 * @Title: login
	 * @Description: 用户登录
	 * @param userInfo 页面的参数
	 * @return: UserInfo
	 */
	public UserInfo login(UserInfo userInfo) {
		UserInfoExample userInfoExample = new UserInfoExample();
		Criteria criteria = userInfoExample.createCriteria();
		criteria.andLoginAccountEqualTo(userInfo.getLoginAccount());
		criteria.andDelFlagEqualTo(1);
		List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
		if (CollectionUtils.isNotEmpty(userInfos)) {
			UserInfo user = userInfos.get(0);
			if (user.getPassword().equals(MD5Utils.Encrypt(userInfo.getPassword(), true))) {
				return user;
			}
			return null;
		}
		return null;
	}
	/**
	 * @Title: saveToken
	 * @Description: 保存用户登录时的token
	 * @param uId 用户ID
	 * @param account 用户账号
	 * @param token 用户token
	 * @return: void
	 */
	public void saveToken(Integer uId, String account, String token) {
		AccessExample accessExample = new AccessExample();
		com.leadingsoft.core.pojo.AccessExample.Criteria criteria = accessExample.createCriteria();
		criteria.andUserIdEqualTo(uId);
		criteria.andUserAccountEqualTo(account);
		criteria.andTokenEqualTo(token);
		List<Access> accesses = accessMapper.selectByExample(accessExample);
		if (CollectionUtils.isNotEmpty(accesses)) {// 如果之前有登录数据要更新
			Access access = accesses.get(0);
			long nowTime = new Date().getTime();
			long endTime = nowTime + (24 * 60 * 60 * 1000);
			access.setEndTime(new Date(endTime));
			access.setDelFlag(1);
			accessMapper.updateByPrimaryKey(access);
		} else {
			Access access = new Access();
			access.setDelFlag(1);
			access.setToken(token);
			access.setUserAccount(account);
			access.setUserId(uId);
			long nowTime = new Date().getTime();
			long endTime = nowTime + (24 * 60 * 60 * 1000);
			access.setEndTime(new Date(endTime));
			accessMapper.insert(access);
		}
	}
	/**
	 * @Title: logout
	 * @Description: 退出登录
	 * @param token 用户验证信息
	 * @return: void
	 */
	public void logout(String token) {
		// 用户账号唯一，所以token也唯一
		Access access = getAccess(token);
		if (null != access) {
			access.setDelFlag(0);
			accessMapper.updateByPrimaryKey(access);
		}
	}
	/**
	 * @Title: getUserInfo
	 * @Description: 通过用户账号获取时间
	 * @param account
	 * @return
	 * @return: UserInfo
	 */
	public UserInfo getUserInfo(String account) {
		UserInfoExample userInfoExample = new UserInfoExample();
		Criteria criteria = userInfoExample.createCriteria();
		criteria.andLoginAccountEqualTo(account);
		criteria.andDelFlagEqualTo(1);
		List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
		if (CollectionUtils.isNotEmpty(userInfos)) {
			return userInfos.get(0);
		}
		return null;
	}
	/**
	 * @Title: getAccess
	 * @Description: 获取用户登录状态
	 * @param token
	 * @return
	 * @return: Access
	 */
	public Access getAccess(String token) {
		AccessExample accessExample = new AccessExample();
		com.leadingsoft.core.pojo.AccessExample.Criteria criteria = accessExample.createCriteria();
		criteria.andTokenEqualTo(token);
		criteria.andDelFlagEqualTo(1);
		List<Access> accesses = accessMapper.selectByExample(accessExample);
		if (CollectionUtils.isNotEmpty(accesses)) {
			return accesses.get(0);
		}
		return null;
	}
	/**
	 * @Title: getUserByAccount
	 * @Description: 通过用户账号获取用户实体
	 * @param account
	 * @return
	 * @return: UserInfo
	 */
	public UserInfo getUserByAccount(String account) {
		UserInfoExample userInfoExample = new UserInfoExample();
		Criteria criteria = userInfoExample.createCriteria();
		criteria.andLoginAccountEqualTo(account);
		criteria.andDelFlagEqualTo(1);
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
		if (CollectionUtils.isNotEmpty(userInfoList)) {
			return userInfoList.get(0);
		}
		return null;
	}
}
